<template>
  <div>
    <template v-for="item in reviewList">
      <good-review-show-item :key="item['id']" :data="item" />
    </template>
  </div>
</template>

<script>
import PageDTO from '@/api/entity/PageDTO'
import SimpleReviewQueryDTO from '@/api/entity/SimpleReviewQueryDTO'
import { queryReviewWrapperPage } from '@/api/ApiSimpleReview'
import { ResultCode } from '@/constants/constants'
import GoodReviewShowItem from '@/views/part/shopDetail/part/goodInfo/part/tableReviews/GoodReviewShowItem'
import { currentPraiseFlagByIds } from '@/api/ApiSimpleReviewPraise'

export default {
  name: 'GoodReviewShowBox',
  components: { GoodReviewShowItem },
  props: {
    goodId: {
      required: true,
      type: [Number, String]
    }
  },
  data () {
    return {
      queryLoading: false,
      reviewList: []
    }
  },
  computed: {
    trueGoodId () {
      return this.goodId && this.goodId > 0
    }
  },
  methods: {
    freshData () {
      if (!this.trueGoodId) {
        return
      }
      this.queryLoading = true
      const pageDTO = PageDTO.instance()
      pageDTO.setCurrent(1)
      pageDTO.setSize(10)
      const wrapper = SimpleReviewQueryDTO.instance()
      wrapper.addGoodId(this.goodId)
      wrapper.addOrderDesc('praise')
      pageDTO.setCondition(wrapper)
      queryReviewWrapperPage(pageDTO, data => {
        if (data && data.code === ResultCode.OK) {
          data = data.object['records']
          if (data && typeof data === 'object' && data.length > 0) {
            const ids = data.map(item => {
              return item.id
            })
            currentPraiseFlagByIds(ids, data2 => {
              if (data2 && data2.code === ResultCode.OK) {
                data2 = data2.object
                for (let i = 0; i < data.length; i++) {
                  data[i].falg = data2[data[i].id + '']
                }
                this.$emit('countChange', data.length)
                this.reviewList = data
              }
            })
          } else {
            this.reviewList = []
            this.$emit('countChange', 0)
          }
        }
      }, () => {
        this.queryLoading = false
      })
    }
  },
  watch: {
    goodId () {
      this.freshData()
    }
  }
}
</script>

<style scoped>

</style>
